package main

import (
	"ChdChat-Gin/router"
	"ChdChat-Gin/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	r := gin.Default()
	if err := initSystem(r); err != nil {
		log.Printf("main.initSystem.%v\n", err)
		return
	}
	router.SystemRouter(r)
	if err := r.Run("127.0.0.1:8081"); err != nil {
		log.Printf("main.gin.Run:%v\n", err)
		return
	}
}

func initSystem(r *gin.Engine) error {
	// Create a log file
	f, _ := os.Create("Monopoly.log")
	gin.DefaultWriter = io.MultiWriter(f, os.Stdout)

	// Create a static address for a file
	r.StaticFS("src", http.Dir("/usr/local/Chd-chat/avatars/src"))

	if err := service.InitChatRoomManager(); err != nil {
		return fmt.Errorf("InitChatRoomMranager:%v\n", err)
	}

	if err := service.InitGameRoomManager(); err != nil {
		return fmt.Errorf("InitGameRoomManager:%v\n", err)
	}
	service.OpenChatRoom()
	return nil
}
